/**
 * Created by admin on 2017/6/5.
 */
import {
  CouponDetail
} from '../mutation-types';
import { Cache, cache_keys } from '../../utils/util';

//查询我的野练PASS列表
export const queryCouponList =function({commit,state},data){

   let index=data.index || 0;
   let page=data.page || 1;
   let params ={};
   params.type =index+1;
   params.page =page;
   let oauth =Cache.get(cache_keys.token);
   EventBus.$http.get(EventBus.$Config.baseUrl+'api/activity/coupon/list',{
     headers:{
        Authorization:oauth.access_token
     },
     params
   }).then(res=>{
     if(res.data.status){
        EventBus.$emit('coupon-list',res.data,index);
     }
     else{
        EventBus.$Modal.info({title:"消息提示",body:res.data.message || "加载错误"});
     }
   },()=>{
        EventBus.$Modal.info({title:"消息提示",body:'加载失败'});
   });
};

// 查询我的野练PASS详情
export const queryCouponDetail =function({commit,state},id){
  var oauth = Cache.get(cache_keys.token);
  EventBus.$http.get(EventBus.$Config.baseUrl+'api/activity/coupon/show/'+id,{
    headers:{
      Authorization: oauth.access_token
    }
  }).then(res=>{
     if(res.data.status){
       var data =res.data.data;
       commit(CouponDetail,data);
     }
     else{
       EventBus.$Modal.info({title:"消息提示",body:res.data.message || '加载错误'});
     }
  },()=>{
    EventBus.$Modal.info({title:"消息提示",body:'加载失败'});
  });
};

//微信是获取野练PASS
export const wxGetCoupon =function({commit,state}){
  let oauth=Cache.get(cache_keys.token);
  EventBus.$http.post(EventBus.$Config.baseUrl+'api/activity/coupon/wxGetCoupon',{},{
    headers:{
       Authorization:oauth.access_token
    }
  }).then(res=>{
     res=res.data;
     EventBus.$emit('gain-complete',res);
  },()=>{
     EventBus.$Modal.info({title:"消息提示",body:'加载失败'});
  })
};

//微信是否可领取野练PASS
export const queryCoupon =function({commit,state}){
  let oauth=Cache.get(cache_keys.token);
  EventBus.$http.get(EventBus.$Config.baseUrl+'api/activity/coupon/wxCheckCoupon',{
    headers: {
      Authorization: oauth.access_token
    }
  }).then(res=>{
     res=res.data;
     if(res.status){
       EventBus.$emit('coupon-check', res.data);
     }
     else{
       EventBus.$Modal.info({title:"消息提示",body:res.message || '加载错误'});
     }
  },()=>{
    EventBus.$Modal.info({title:"消息提示",body:'加载失败'});
  }).then( ()=>{
     EventBus.$emit('show-result');
  });
};
